//
//  Func_iOS.m
//  Demo
//
//  Created by migame on 15/12/14.
//
//

#include "MyObj.h"




MyString* MyString::create(const std::string& str)
{
    MyString* ret = (MyString*) new __String(str);
    return ret;
}

MyString* MyString::createWithFormat(const char* format, ...)  //CC_FORMAT_PRINTF(1, 2)
{
    MyString* ret = MyString::create("");
    va_list ap;
    va_start(ap, format);
    ret->initWithFormatAndValist(format, ap);
    va_end(ap);

    return ret;
}

MyString* MyString::createWithData(const unsigned char* pData, size_t nLen)
{
    MyString* ret = nullptr;
    if (pData != nullptr)
    {
        char* pStr = (char*)malloc(nLen+1);
        if (pStr != nullptr)
        {
            pStr[nLen] = '\0';
            if (nLen > 0)
            {
                memcpy(pStr, pData, nLen);
            }

            ret = MyString::create(pStr);
            free(pStr);
        }
    }
    return ret;
}

MyString* MyString::createWithContentsOfFile(const std::string& filename)
{
    std::string str = FileUtils::getInstance()->getStringFromFile(filename);
    return MyString::create(std::move(str));
}





MyArray* MyArray::create()
{
    MyArray* array = new MyArray();
    if (array && array->initWithCapacity(7))
    {}
    else{
        CC_SAFE_DELETE(array);
    }
    return array;
}

MyArray* MyArray::createWithObject(Ref* object)
{
    MyArray* array = new MyArray();

    if (array && array->initWithObject(object)){}
    else{
        CC_SAFE_DELETE(array);
    }
    return array;
}

MyArray* MyArray::create(Ref* object, ...)
{
    va_list args;
    va_start(args,object);
    MyArray* array = create();
    if (array && object)
    {
        array->addObject(object);
        Ref *i = va_arg(args, Ref*);
        while (i)
        {
            array->addObject(i);
            i = va_arg(args, Ref*);
        }
    }
    else
    {
        CC_SAFE_DELETE(array);
    }
    va_end(args);
    return array;
}


MyArray* MyArray::createWithArray(MyArray* otherArray)
{
    return (MyArray*)otherArray->clone();
}

MyArray* MyArray::createWithCapacity(ssize_t capacity)
{
    CCASSERT(capacity>=0, "Invalid capacity");
    MyArray* array = new MyArray();
    if (array && array->initWithCapacity(capacity))
    {}
    else{
        CC_SAFE_DELETE(array);
    }
    return array;
}

MyArray* MyArray::createWithContentsOfFile(const std::string& fileName)
{
    MyArray* ret = MyArray::createWithContentsOfFileThreadSafe(fileName);
    return ret;
}

MyArray* MyArray::createWithContentsOfFileThreadSafe(const std::string& fileName)
{
    ValueVector arr = FileUtils::getInstance()->getValueVectorFromFile(fileName);
    MyArray* ret = MyArray::createWithCapacity(static_cast<int>(arr.size()));
    for(const auto &value : arr) {
        ret->addObject(MyString::create(value.asString()));
    }
    return ret;
}





MyDictionary* MyDictionary::create()
{
    MyDictionary* ret = new MyDictionary();
    if (ret && ret->init() )
    {}
    return ret;
}

MyDictionary* MyDictionary::createWithDictionary(__Dictionary* srcDict)
{
    return (MyDictionary*)srcDict->clone();
}


//MyDictionary* MyDictionary::createWithContentsOfFileThreadSafe(const char *pFileName)
//{
//    MyDictionary *dic = MyDictionary::createWithContentsOfFileThreadSafe(pFileName);
//    return MyDictionary::createWithDictionary(dic);
//}


MyDictionary* MyDictionary::createWithContentsOfFile(const char *pFileName)
{
    MyDictionary *ret = (MyDictionary *)MyDictionary::createWithContentsOfFileThreadSafe(pFileName);
    if (ret != nullptr)
    {
    }
    return ret;
}













